class Solution {
public:
    bool check(TreeNode* root,TreeNode* subRoot){
        if(!root && !subRoot){
            return true;
        }
        if(root && !subRoot || !root && subRoot || root->val != subRoot->val){
            return false;
        }
        return check(root->left,subRoot->left) && check(root->right,subRoot->right);
    }
                                        
    bool isSubtree(TreeNode* root, TreeNode* subRoot) {
        if(!subRoot){
            return true;
        }
        if(subRoot && !root){
            return false;
        }
        if(subRoot->val == root->val){
            if(check(root,subRoot)){
                return true;
            }
        }
        
        if(isSubtree(root->left,subRoot)){
            return true;
        }
        if(isSubtree(root->right,subRoot)){
            return true;
        }
        return false;
    }
};
